package com.aistar.mapper.jdbc;

import com.aistar.mapper.MemberMapper;

import com.aistar.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

//8// <bean id="memberMapperImpl" class="com.aistar.mapper.MemberMapperImpl">
public class MemberMapperImpl implements MemberMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public Member selectByPrimaryKey(int id) {
        RowMapper<Member> rowMapper =  new BeanPropertyRowMapper<>(Member.class);
        String sql ="select * from member where member_id = ?";
        Object[] args = {id};
        Member member = jdbcTemplate.queryForObject(sql,args,rowMapper);
        return member;
    }

    @Override
    public int save(Member member) {
        String sql ="insert into member(member_name,member_telno,password,postbox,score,status,other1,other2) values(?,?,?,?,?,?,?,?)";
        Object[] args ={member.getMemberName(),member.getMemberTelno(),member.getPassword(),member.getPostbox(),member.getScore(),member.getStatus(),member.getOther1(),member.getOther2()};

        System.out.println("mapper 实现insert");
        return this.jdbcTemplate.update(sql,args);
    }

    @Override
    public int update(Member member) {
        String sql = "update member set member_name = ?,member_telno= ?,password= ?,postbox= ?,score= ?,status= ?,other1= ?,other2 = ? where member_id = ?";
        Object[] agrs = {member.getMemberName(),member.getMemberTelno(),member.getPassword(),member.getPostbox(),member.getScore(),member.getStatus(),member.getOther1(),member.getOther2(),member.getMemberId()};
        System.out.println("mapper 实现 update");
        return this.jdbcTemplate.update(sql,agrs);
    }


    @Override
    public int delete(int id) {
        String sql="delete from member where member_id = ?";
        Object[] args = {id};
        System.out.println("mapper 实现 delete");
        return this.jdbcTemplate.update(sql,args);
    }


    @Override
    public int selectByOthers(String memberName, String memberPwd) {
        System.out.println("mapper 实现 selectByOthers");
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Member member1) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(int id) {
        return 0;
    }

    @Override

        public int updateScore(Member member) {
            String sql = "update member set score = ?  where member_id = ?";
            Object[] args = {member.getScore(),member.getMemberId()};
            System.out.println("修改积分");
            return this.jdbcTemplate.update(sql,args);
        }
}
